package com.eshop.we.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.eshop.we.book.entity.Book;
import com.eshop.we.business.BookBusiness;
import com.eshop.we.business.FavoriteBusiness;
import com.eshop.we.favorite.entity.Favorite;
import com.eshop.we.result.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/favorite")
public class FavoriteController extends  BaseController{

     @Autowired
    FavoriteBusiness favoriteBusiness;

     @Autowired
    BookBusiness bookBusiness;


    @ApiOperation(value = "查询收藏用户",notes = "查询收藏用户")
    @GetMapping("/getfavorite")
     public Result getFavorite(@ApiIgnore Integer userId){
       return  Result.success(favoriteBusiness.selectUserBooks(userId));
     }

    @ApiOperation(value = "查询收藏用户的书籍",notes = "查询收藏用户的书籍")
    @GetMapping("/getfavoritebooks")
     public Result getAllBoolks(@RequestParam(required = true) Integer favoriteId){
        return  Result.success(bookBusiness.selectList(new EntityWrapper<Book>().eq("user_id",favoriteId).eq("is_del",0).eq("status",10)));
     }


    @ApiOperation(value = "移除收藏用户",notes = "移除收藏用户")
    @PutMapping("/delfavorite")
     public Result delFavorite(@ApiParam(value = "收藏用户id",required = true) @RequestParam("favoriteId") Integer favoriteId, @ApiIgnore Integer userId){
        return Result.success(favoriteBusiness.updateFavoriteStatus(favoriteId, userId));
     }

     @PostMapping("/addfavorite")
     public Result addFavorite(@RequestParam(required = true) Integer fuserId,@RequestParam(required = true) Integer userId){
         Favorite favorite=new Favorite();
         favorite.setType(0);
         favorite.setCtime((int)(System.currentTimeMillis()/1000));
         favorite.setUserId(userId);
         favorite.setFavoriteId(fuserId);
         return Result.success(favoriteBusiness.insert(favorite));
     }
}
